package ttp.service;

import java.util.HashMap;

import ttp.dao.TtpDAO;
import ttp.to.ChemicalTO;
import ttp.to.CrushTO;
import ttp.utilities.Utilities;

public class ChemicalService {
	private TtpDAO dao;

	public ChemicalService(TtpDAO dao) {
		super();
		this.dao = dao;
	}

	public HashMap run() throws Exception{
		long start_time = System.currentTimeMillis();
		ChemicalTO cmto = new ChemicalTO();
		CrushTO cto = dao.selectCrushByNo(0);
		if(cto!=null){
			int chemicalNo = dao.sequenceChemical();
			String enterPrise = cto.getEnterPrise();
			String treeType = cto.getTreeType();
			String treeGrade = cto.getTreeGrade();
			int treeWeight = cto.getTreeWeight();
			int pulpWeight = treeWeight - (treeWeight/10);
			String productDate = Utilities.getNow();
			int chemicalCheck = (int) (Math.random()*100)+1;
			if(chemicalCheck==100){
				chemicalCheck = 1;
			}else{
				chemicalCheck = 0;
			}
			cmto = new ChemicalTO(chemicalNo, enterPrise, treeType, treeGrade, pulpWeight, productDate, chemicalCheck, 0, 0);
			dao.insertChemical(cmto);
			dao.updateCrush(cto.getCrushNo());	
			Thread.sleep(4000);
		}else if(cto==null){
			Thread.sleep(2000);
			cmto = null;
		}
		long end_time = System.currentTimeMillis();
		System.out.println("Chemical 처리시간: " + String.format("%,d msec", end_time - start_time));
		HashMap map = new HashMap();
		map.put("cmto", cmto);
		map.put("time", String.format("%,d msec", end_time - start_time));
		return map;
	}
}
